package com.example.myapplication.room;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import java.util.List;

public class LocaRoomRequestManager implements IDatabaseRequest {


    private static LocaRoomRequestManager INSTANCE;
    private StudentDao studentDao;
    private static LiveData<List<Student>> studentsLive;

    private LocaRoomRequestManager (Context mContext){
        StudentDatabase studentDatabase = StudentDatabase.getDatabase(mContext);
        studentDao = studentDatabase.getStudentDao();
        studentsLive = studentDao.getAllStudentLive();
    }


    public static LocaRoomRequestManager getInstance(Context mContext) {
        if (INSTANCE == null) {
            synchronized (LocaRoomRequestManager.class){
                if (INSTANCE == null) {
                    INSTANCE = new LocaRoomRequestManager(mContext);
                }
            }
        }
        return INSTANCE;
    }

    public LiveData<List<Student>> getStudentLive(){
        return studentsLive;
    }

    @Override
    public void insertStudents(Student... students) {
        new InsertAsyscTask(studentDao).execute(students);
    }

    @Override
    public void updateStudents(Student... students) {
         new UpdateAsyscTask(studentDao).execute(students);
    }

    @Override
    public void deleteStudents(Student... students) {
         new DeleteAsyscTask(studentDao).execute(students);
    }

    @Override
    public void deleteAllStudents() {
         new DeleteAllAsyscTask(studentDao).execute();
    }


    static class InsertAsyscTask extends AsyncTask<Student,Void,Void> {
       private StudentDao studentDao;

       InsertAsyscTask(StudentDao studentDao) {
           this.studentDao = studentDao;
       }

        @Override
        protected Void doInBackground(Student... students) {
           studentDao.insertWords(students);//  就这一句  dao  增、删、改、查
            return null;
        }
    }


    static class UpdateAsyscTask extends AsyncTask<Student,Void,Void> {
        private StudentDao studentDao;

        UpdateAsyscTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }

        @Override
        protected Void doInBackground(Student... students) {
            studentDao.updateWords(students);//  就这一句  dao  增、删、改、查
            return null;
        }
    }

    static class DeleteAsyscTask extends AsyncTask<Student,Void,Void> {
        private StudentDao studentDao;

        DeleteAsyscTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }

        @Override
        protected Void doInBackground(Student... students) {
            studentDao.deleteWrods(students);//  就这一句  dao  增、删、改、查
            return null;
        }
    }

    static class DeleteAllAsyscTask extends AsyncTask<Void,Void,Void> {
        private StudentDao studentDao;

        DeleteAllAsyscTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            studentDao.deleteAllWords();//  就这一句  dao  增、删、改、查
            return null;
        }

    }

}
